package org.sunflower.framework.utils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.util.CharsetUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class HttpUtils
{
	static Map<String, String> contentTypes = new HashMap<String, String>();

	static
	{
		contentTypes.put(".*", "application/octet-stream");
		contentTypes.put(".jpg", "image/jpeg");
	}

	public static String getContentType(String fileName)
	{
		if (!contentTypes.containsKey(fileName)) return contentTypes.get(".*");
		return contentTypes.get(fileName);
	}

	/**
	 * get 请求 url 获取的 Json 对象
	 * 
	 * @param url
	 * @return WXBase.constainsKey(json) == false 则 返回 null
	 */
	public static JSONObject GetMethodToJson(String url, Object... urlArgs)
	{
		JSONObject json = JSON.parseObject(GetMethod(urlArgs == null ? url : String.format(url, urlArgs)));
		return printError(json) ? json : null;
	}

	/**
	 * post 请求 url 获取的 Json 对象
	 * 
	 * @param url
	 * @return WXBase.printError(json) == false 则 返回 null
	 */
	public static JSONObject postMethodToJson(String jsonString, String url, Object... urlArgs)
	{
		JSONObject json = JSON.parseObject(postMethod(jsonString, urlArgs == null ? url : String.format(url, urlArgs)));
		return printError(json) ? json : null;
	}

	/**
	 * post 请求 url 获取的 Json 对象
	 * 
	 * @param url
	 * @return WXBase.constainsKey(json) == false 则 返回 null
	 */
	public static JSONObject postMethodToJson(String url, File file)
	{
		JSONObject json = JSON.parseObject(postMethod(url, file));
		return printError(json) ? json : null;
	}

	/**
	 * get 请求 url 返回 responseText
	 * 
	 * @param url
	 * @return
	 */
	public static String GetMethod(String url)
	{
		ConsoleUtils.WriteLine("get: %s", url);
		
		String responseText = null;
		
		try
		{
			responseText = Request.Get(url).setHeader("ContentType", "application/json;utf-8").execute().returnContent().asString(CharsetUtils.get("utf-8"));
		}
		catch (Exception ex)
		{
		}

		return responseText;
	}

	/**
	 * post 请求 url 返回 responseText
	 * 
	 * @param url
	 * @return
	 */
	public static String postMethod(String jsonString, String url)
	{
		ConsoleUtils.WriteLine("post: %s\r\n          body: %s", url, jsonString);
		
		String responseText = null;
		ContentType ct = ContentType.create("application/json", "utf-8");
		try
		{
			responseText = Request.Post(url).bodyString(jsonString, ct).execute().returnContent().asString(CharsetUtils.get("utf-8"));
		}
		catch (Exception ex)
		{
		}

		return responseText;
	}

	/**
	 * post 请求 url 提交 文件
	 * 
	 * @param url
	 * @param file
	 * @return
	 */
	public static String postMethod(String url, File file)
	{
		ConsoleUtils.WriteLine("post: %s\r\n          filePath: %s", url, file.getAbsolutePath());
		
		String responseText = null;
		
		if (file.exists())
		{
			MultipartEntityBuilder meb = MultipartEntityBuilder.create();
			meb.addBinaryBody(file.getName(), file);
			HttpEntity entity = meb.build();
			try
			{
				responseText = Request.Post(url).body(entity).execute().returnContent().asString(CharsetUtils.get("utf-8"));
			}
			catch (Exception ex)
			{
			}
		}

		return responseText;
	}
	
	/**
	 * 如果 errcode > 0  执行失败， 返回  false 打印错误信息
	 * 
	 * @param json
	 * @return
	 */
	public static boolean printError(JSONObject json)
	{
		if (json != null && json.containsKey("errcode"))
		{
			int err = json.getInteger("errcode");
			if (err > 0)
			{
				ConsoleUtils.WriteLine(json);
				return false;
			}
		}

		return true;
	}
}
